Output

sealed class Output<T>

Structured output for generateText / streamText / Agent.

Per invariant I-3 / I-6, structured output goes through Output. Prefer generateText(output = ...) / streamText(output = ...). generateObject() and streamObject() are compatibility shims for v6 call sites that still use the deprecated object helpers.

Four variants:

  • Obj — single typed object (Kotlin-keyword-safe rename of v6's object).

  • Arr — array of typed objects.

  • Choice — constrained enum / set of allowed string values.

  • JsonTree — generic JSON tree, untyped.

Idiomatic use:

val recipe = generateText(
model = ...,
prompt = "Generate a chocolate cake recipe",
output = Output.obj(serializer<Recipe>()),
).output

Inheritors

Types

Link copied to clipboard
class Arr<T>(val elementSerializer: KSerializer<T>, val name: String = elementSerializer.descriptor.serialName.substringAfterLast('.') + "[]", val schemaDescription: String? = null) : Output<List<T>>
Link copied to clipboard
class Choice<T>(val options: List<T>, val encode: (T) -> String, val decodeChoice: (String) -> T, val name: String = "choice", val schemaDescription: String? = null) : Output<T>
Link copied to clipboard
object Companion
Link copied to clipboard
class JsonTree(val name: String = "json", val schemaDescription: String? = null) : Output<JsonElement>
Link copied to clipboard
class Obj<T>(val serializer: KSerializer<T>, val name: String = serializer.descriptor.serialName.substringAfterLast('.'), val schemaDescription: String? = null) : Output<T>

Output.object() in v6, renamed to obj because object is a Kotlin keyword.

Properties

Link copied to clipboard
open val schema: JsonElement
Link copied to clipboard
open val schemaDescription: String? = null
Link copied to clipboard
abstract val schemaJson: String
Link copied to clipboard
abstract val schemaName: String

Functions

Link copied to clipboard
abstract fun decode(text: String): T